//
//  DetailTableViewController.m
//  ShowMe
//
//  Created by 123 on 2018/8/28.
//  Copyright © 2018年 nkl. All rights reserved.
//

#import "DetailTableViewController.h"
#import "AnnouncesModel.h"
#import <WordPressEditor/WPEditorViewController.h>
#import <WordPressEditor/WPEditorView.h>
#import "KLWriteArticleViewController.h"
#import "ShowArtDetailVC.h"
@interface DetailTableViewController ()<WPEditorViewControllerDelegate>
@property(nonatomic,strong) NSNumber * classType;
@property(nonatomic,strong) NSMutableArray * dataArray;
@property(nonatomic,weak) WPEditorView * wpView;
@property(nonatomic,strong) WPEditorViewController * wpvc;
@property(nonatomic,strong) NSIndexPath * m_indexPath;
@end

@implementation DetailTableViewController

#pragma mark - 懒加载
-(NSNumber *)classType{
    if (!_classType) {
        _classType = [NSNumber numberWithInt:0];
    }
    return _classType;
}

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

-(WPEditorViewController *)wpvc{
    if(!_wpvc){
        _wpvc = [[ShowArtDetailVC alloc] initWithMode:kWPEditorViewControllerModePreview];
    }
    return _wpvc;
}
-(NSIndexPath *)m_indexPath{
    if (!_m_indexPath) {
        _m_indexPath = [[NSIndexPath alloc] init];
    }
    return _m_indexPath;
}
#pragma mark - 初始化
- (void)viewDidLoad {
    [super viewDidLoad];

    [self requestList];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setMdataSource:(NSNumber *)index{
    self.classType = index;
}

#pragma mark - 请求数据
-(void)requestList{
    BmobQuery * query = [BmobQuery queryWithClassName:__Announces__];
    
//    [query whereKey:__Ann_classid__ equalTo:self.classType];
    NSArray * array = @[@{__Ann_classid__ : self.classType},@{@"isVisiable":@"true"}];
    [query addTheConstraintByAndOperationWithArray:array];
    [query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        for (BmobObject * objc in array) {
            AnnouncesModel * model = [[AnnouncesModel alloc] initFromBmobObject:objc];
            [self.dataArray addObject:model];
        }
    
        [self.tableView reloadData];
    }];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * cellID = @"cellID";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.textLabel.text = [self.dataArray[indexPath.row] topic];
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ShowArtDetailVC * wpvc = [[ShowArtDetailVC alloc]initWithMode:kWPEditorViewControllerModePreview];
    wpvc.delegate = self;
    //记录行数
    self.m_indexPath = indexPath;
    [self.navigationController pushViewController:wpvc animated:YES];
}

#pragma mark - 必须实现代理方法才可以使用setBodyText方法
-(void)editorDidFinishLoadingDOM:(WPEditorViewController *)editorController{
    [editorController setTitleText:[self.dataArray[self.m_indexPath.row] topic]];
    [editorController setBodyText:[self.dataArray[self.m_indexPath.row] Body]];
    CGFloat documentHeight = [[editorController.editorView.webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"] floatValue];
    ShowArtDetailVC * vc = (ShowArtDetailVC *)editorController;
    [vc setHeaderHeight:documentHeight];
    
    NSLog(@"height======%@",NSStringFromCGRect(vc.editorView.frame));
    
}



@end
